#include "server_config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "debug_assert.h"
#include "debug_log.h"

void GetWorkPath(char *p_work_path, const int path_size) {
	ssize_t act_size;
	char *p_find;

	ASSERT(NULL != p_work_path && 0 < path_size);
	memset(p_work_path, 0, path_size);
	act_size = readlink("/proc/self/exe", p_work_path, path_size);
	ASSERT(-1 != act_size);
	//printf("work_path=%s\n", p_work_path);
	p_find = strrchr(p_work_path, '/');
	ASSERT(NULL != p_find);
	*p_find = 0;
}

void GetConfigPath(char *p_config_path, const int path_size) {
	char work_path[256];
	char config_path[256];
	int config_path_len;

	ASSERT(NULL != config_path && 0 != path_size);
	GetWorkPath(work_path, sizeof(work_path));
	strcpy(config_path, work_path);
	strcat(config_path, "/AlarmSocketServer.conf");
	config_path_len = strlen(config_path);
	ASSERT(config_path_len < path_size);
	strcpy(p_config_path, config_path);
}

static int GetPrivateProfileString(const char *p_app_name, const char *p_key_name, const char *p_default, char *p_returned_string, int returned_string_size, const char *p_file_name) {
	FILE *pf_ini;
	char line[256];
	char *p_find_app;
	char *p_find_key;
	int returned_length;
	int dw_result;

	ASSERT(NULL != p_app_name && NULL != p_key_name);
	ASSERT(NULL != p_returned_string && NULL != p_file_name);
	pf_ini = fopen(p_file_name, "rb");
	if (NULL == pf_ini)
		dw_result = 0;
	else {
		p_find_app = NULL;
		p_find_key = NULL;
		while (true) {
			fgets(line, sizeof(line), pf_ini);
			if (feof(pf_ini))
				break;
			else {
				if (NULL == p_find_app)
					p_find_app = strstr(line, p_app_name);
				else if (NULL == p_find_key) {
					p_find_key = strstr(line, p_key_name);
					if (NULL != p_find_key) {
						sscanf(line, "%*[^=]=%[^ \r\n]", p_returned_string);
						returned_length = strlen(p_returned_string);
						if (0 >= returned_length || returned_length >= returned_string_size)
							dw_result = 0;
						else
							dw_result = returned_length + 1;
						break;
					}
					else
						continue;
				}
			}
		}
		fclose(pf_ini);
	}
	return dw_result;	
}

void GetSocketIp(const char *p_config_path, char *p_ip, const int ip_size) {
	char ip[20];
	int copy_byte;

	ASSERT(NULL != p_config_path && NULL != p_ip);
	copy_byte = GetPrivateProfileString("socket", "ip", NULL, ip, sizeof(ip), p_config_path);	
	ASSERT(0 < copy_byte && ip_size > copy_byte);
	strcpy(p_ip, ip);
}

void GetSocketPort(const char *p_config_path, int *p_port) {
	char port[8];
	int copy_byte;

	ASSERT(NULL != p_config_path && NULL != p_port);
	copy_byte = GetPrivateProfileString("socket", "port", NULL, port, sizeof(port), p_config_path);
	ASSERT(0 != copy_byte);
	*p_port = atoi(port);
}

void GetMysqlHost(const char *p_config_path, char *p_host, const int host_size) {
	char host[20];
	int copy_byte;

	ASSERT(NULL != p_config_path && NULL != p_host);
	copy_byte = GetPrivateProfileString("mysql", "host", NULL, host, sizeof(host), p_config_path);	
	ASSERT(0 < copy_byte && host_size > copy_byte);
	strcpy(p_host, host);
}

void GetMysqlPort(const char *p_config_path, int *p_port) {
	char port[8];
	int copy_byte;

	ASSERT(NULL != p_config_path && NULL != p_port);
	copy_byte = GetPrivateProfileString("mysql", "port", NULL, port, sizeof(port), p_config_path);
	ASSERT(0 != copy_byte);
	*p_port = atoi(port);
}


